var config = require("../../config");

Page({
  data: {
    Cancellation_Reasons: ["报价太高了", "需要重新修订订单信息", "重新恢复正常了", "等待时间太长", "不放心取机维修", "不想到店维修"],
    orderstatus1: ["邮寄中", "维修中", "维修完成", "回寄中", "取消订单"],
    orderstatus2: ["等待到店维修", "维修中", "维修完成", "等待取货中", "取消订单"],
  },
  onLoad(e) {
    console.log(e);

    let _this = this, orderId = e.orderDetailId || e.orderId, type = e.type;
    wx.getNetworkType({
      success: function (res) {
        if (res.networkType != "none") {
          wx.showLoading({
            title: '加载中...',
            mask: true
          });
          wx.request({
            method: 'POST',
            url: config.service.orderDetail,
            data: {
              uid: wx.getStorageSync("loginInfo").uid,
              logintoken: wx.getStorageSync("loginInfo").token,
              order_id: orderId
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
              console.log(res);
              wx.hideLoading();
              if (res.data.code == 1) {
                _this.setData({
                  orderDetailInfo: res.data.data,
                  orderId: res.data.data.id,
                  type
                });
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: "loading",
                  duration: 2000,
                  mask: true
                });
              }
            }
          });
        } else {
          wx.showToast({
            title: "好像没网了...",
            icon: "loading",
            duration: 2000,
            mask: true
          });
        }
      },
    });
  },
  onShareAppMessage() {

  },
  Cancel() {   //取消订单
  console.log("1111111")
    this.setData({
      CancelOrder: {   //取消订单摸版数据
        hint_CancelOrder: "hint-CancelOrder",
        Event: {
          formReset: "cancel",
          formSubmit: "confirm"
        }
      }
    });

  },
  cancel(e) {
    this.setData({
      CancelOrder: {   //取消订单摸版数据
        hint_CancelOrder: ""
      }
    });
  },
  confirm(e) {
    let formData = e.detail.value, td = this.data;
    if (formData.radio_group || formData.textarea != "") {
      this.setData({
        CancelOrder: {   //取消订单摸版数据
          hint_CancelOrder: ""
        }
      });
      Cancel({
        _this: this,
        orderId: td.orderId,
        cancel_reason1: td.Cancellation_Reasons[formData.radio_group],
        cancel_reason2: formData.textarea
      });
    } else {
      wx.showToast({
        title: "还不知道原因呢",
        icon: "loading",
        duration: 2000,
        mask: true
      });
    }

  },
  home(){
    wx.navigateBack({
      delta: 3
    });
  }
});

function Cancel(obj) {
  let _this = obj._this;
  wx.getNetworkType({
    success: function (res) {
      if (res.networkType != "none") {
        wx.showLoading({
          title: '取消中...',
          mask: true
        });
        wx.request({
          method: 'POST',
          url: config.service.cancelAnOrder,
          data: {
            uid: wx.getStorageSync("loginInfo").uid,
            logintoken: wx.getStorageSync("loginInfo").token,
            order_id: obj.orderId,
            cancel_reason1: obj.cancel_reason1,
            cancel_reason2: obj.cancel_reason2
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success(res) {
            wx.hideLoading();
            console.log(res);
            if (res.data.code == 1) {
              wx.showToast({
                title: res.data.msg,
                icon: "success",
                duration: 2000,
                mask: true
              });
              setTimeout(function () {
                wx.navigateBack();
              }, 1000);
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: "loading",
                duration: 2000,
                mask: true
              });
            }
          }
        });
      } else {
        wx.showToast({
          title: "好像没网了...",
          icon: "loading",
          duration: 2000,
          mask: true
        });
      }
    },
  });
}